package com.bckj.freshnow.order.trade.ons.listener.strategy;

import com.alibaba.cola.statemachine.StateMachineFactory;
import com.bckj.fastboot.core.jackson.JsonUtils;
import com.bckj.fastboot.mq.ons.OnsConsumeStrategy;
import com.bckj.freshnow.common.constant.TradeOrderMQConstants;
import com.bckj.freshnow.order.fsm.enums.OrderStateMachineIdEnum;
import com.bckj.freshnow.order.model.TradeOrderMsg;
import com.bckj.freshnow.order.trade.fsm.TradeOrderContext;
import com.bckj.freshnow.order.trade.fsm.enums.TradeOrderEventEnum;
import com.bckj.freshnow.order.trade.fsm.enums.TradeOrderStatusEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
@RequiredArgsConstructor
public class TradeOrderTimeOutCancelConsumeStrategy implements OnsConsumeStrategy {

    @PostConstruct
    public void register() {
        TradeOrderConsumeStrategyFactory.STRATEGY_MAP.put(getTag(), this);
    }

    @Override
    public String getTag() {
        return TradeOrderMQConstants.TAG_TIMEOUT_CANCEL;
    }

    @Override
    public void consume(String params) {
        TradeOrderMsg tradeOrderMsg =  JsonUtils.toBean(params, TradeOrderMsg.class);
        var ctx = TradeOrderContext.create(tradeOrderMsg.getOrderId(), 0);
        StateMachineFactory.get(OrderStateMachineIdEnum.TRADE_ORDER.getMachineId())
                .fireEvent(TradeOrderStatusEnum.PAY_ONLINE, TradeOrderEventEnum.CANCEL_ORDER, ctx);
    }
}
